<?php

namespace App\Http\Controllers\staff;

use Illuminate\Http\Request;
use DB;
use Cookie;
use Hash;
use App\Http\Controllers\Controller;
//模型
use App\Models\Role;

class StorageController extends Controller
{
    public function getIndex(Request $request){
        $staffInfo = $request -> cookie('staffInfo');
        //获取账号列表
        if($staffInfo['staff_role_id'] == 1){
            //查询仓库表
            $data = DB::table('nb_bind as nb')
                -> leftJoin('room as r','r.id','=','nb.room_id')
                -> leftJoin('type as t','t.id','=','nb.type_id')
                -> leftJoin('nb_device as nd','nd.id','=','nb.device_id')
                -> select('nb.*','r.name as rname','t.name as type_name','nd.verifyCode as verifyCode')
                -> where('nb.agent_id',$staffInfo['agent_id'])
                -> get();
        }else{
            $data = [];
        }
        return view('staff.storage.index',['data'=>$data]);
    }

    public function getCreate(Request $request){
        //查询设备类型
        $type = DB::table('type') -> select('id','name') -> get();
        //查询模组编码
        $verifyCode = DB::table('nb_device') -> select('id','verifyCode') -> get();
        //查询房间表
        $room = DB::table('room') -> select('id','name') -> where('agent_id',$request -> cookie('staffInfo')['agent_id']) -> get();
        return view('staff.storage.create',['type'=>$type,'verifyCode'=> $verifyCode,'room'=>$room]);
    }

    public function postStore(Request $request){
        $info = $request -> except('_token');
        //插入仓库表
        $res = DB::table('nb_bind') -> insert($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        return json_encode($data);
    }

    public function getEdit(Request $request){
        $id = $request -> input('id');
        //查询仓库表
        $data = DB::table('nb_bind as nb')
            -> leftJoin('room as r','r.id','=','nb.room_id')
            -> leftJoin('type as t','t.id','=','nb.type_id')
            -> leftJoin('nb_device as nd','nd.id','=','nb.device_id')
            -> select('nb.*','r.name as rname','t.id as type_id','t.name as type_name','nd.id as device_id','nd.verifyCode')
            -> where('nb.id',$id)
            -> first();
        //查询设备类型
        $type = DB::table('type') -> select('id','name') -> get();
        //查询模组编码
        $verifyCode = DB::table('nb_device') -> select('id','verifyCode') -> get();
        //查询房间表
        $room = DB::table('room') -> select('id','name') -> where('agent_id',$request -> cookie('staffInfo')['agent_id']) -> get();
        return view('staff.storage.edit',['data'=> $data,'type'=>$type,'verifyCode'=> $verifyCode,'room'=>$room]);
    }

    public function postUpdate(Request $request){
        $id = $request -> input('id');
        $info = $request -> except('_token','id');
        //修改仓库表
        $res = DB::table('nb_bind') -> where('id',$id) -> update($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        return json_encode($data);
    }

    public function postDestroy(Request $request){
        $id = $request -> input('id');
        //删除员工表数据
        $res = DB::table('nb_bind') -> delete($id);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        return json_encode($data);
    }
}